/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.compra.controlador;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.clases.Mensaje;
import sv.com.intesal.compra.bean.TipoMaterial;
import sv.com.intesal.compra.sql.SQLTipoMaterial;
import sv.com.intesal.conexion.cConexion;

/**
 *
 * @author JM
 */
public class TipoMaterialControlador {

    public List<TipoMaterial> fillComboBox() throws Exception {

        List<TipoMaterial> list = new ArrayList<TipoMaterial>();
        try {
            ResultSet rs = cConexion.conectar().prepareStatement(SQLTipoMaterial.buscarTipoMateriales).executeQuery();
            while (rs.next()) {
                TipoMaterial tm = new TipoMaterial();
                tm.setCodigo_tm(rs.getInt("codigo_tm"));
                tm.setNombre_tm(rs.getString("nombre_tm"));
                list.add(tm);
            }
            rs.close();
            return list;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public List<TipoMaterial> fillGrid() throws Exception{
        try {
            return this.fillComboBox();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public void mantenimientoTipoMaterial(TipoMaterial tipoMaterial) throws Exception {

        PreparedStatement ps = null;
        Connection cnn = null;
        try {
            cnn = cConexion.conectar();
            if (tipoMaterial.getCodigo_tm() == null) {
                ps = cnn.prepareStatement(SQLTipoMaterial.insertTipoMaterial);
            } else {
                ps = cnn.prepareStatement(SQLTipoMaterial.updateTipoMaterial);
                ps.setInt(2, tipoMaterial.getCodigo_tm());
            }
            ps.setString(1, tipoMaterial.getNombre_tm());
            ps.executeUpdate();

            ps.close();
            cnn.close();

            Mensaje m = new Mensaje();
            if (tipoMaterial.getCodigo_tm() == null) {
                m.messageInfo(m.getIinformation());
            } else {
                m.messageInfo(m.getImodificada());
            }
        } catch (Exception ex) {
            throw new Exception(ex);
        }
    }
}
